package com.scy.boot05.controller;

import com.scy.boot05.entity.User;
import com.scy.boot05.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    UserMapper userMapper;

    @RequestMapping("/reg")
    public String reg(User user){
        System.out.println("user = " + user);
//        通过用户输入的用户名是否存在
        User u = userMapper.selectByUsername(user.getUsername());
        if (u != null){
            return "用户名已存在！！！";
        }
        //用户名不存在的话就可以执行注册流程
        userMapper.insert(user);

        return "注册成功<a href='/'>返回首页</a>";
    }

    @RequestMapping("/login")
    public String login(User user){
        System.out.println("user = " + user);
        //通过客户端传递过来的用户名查询对应的数据
        User u = userMapper.selectByUsername(user.getUsername());
        if (u!=null){//代表查询到了数据
            //判断判断用户输入的密码和数据库的密码是否一致
            if (user.getPassword().equals(u.getPassword())){
                return "登陆成功<a href='/'>返回首页</a>";
            }
            return "密码错误<a href='/'>返回首页</a>";
        }
        return "用户名不存在<a href='/'>返回首页</a>";
    }
}
